﻿using Module.Common.PubEvent;
using Module.Common.Properties;
using DryIoc;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using ServiceItem;
using ServiceItem.Engine;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Collections;
using MaterialDesignThemes.Wpf;
using System.Data;
using Module.Common.Views;

namespace PrismChessApp.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private readonly IRegionManager Rgm;  // 区域管理器
        private readonly IEventAggregator Ea; // 订阅消息管理器
        private readonly IContainerProvider Cp; // 容器
        private IRegionNavigationJournal regionNavigationJournal; // 导航
        public DelegateCommand LastCommand { get; private set; }
        public DelegateCommand NextCommand { get; private set; }
        public DelegateCommand RestartCommand { get; private set; }
        public DelegateCommand FanZhuanCommand { get; private set; }
        public DelegateCommand<string> SubMenuCommand { get; private set; }

        private string _title = "Prism Chess";
        private string currentModuleName;
        private bool isDarkMode;
        public bool IsDarkMode
        {
            get { return isDarkMode; }
            set { 
                SetProperty(ref isDarkMode, value);
                ModifyTheme(value);
            }
        }
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }
        public string CurrentModuleName
        {
            get { return currentModuleName; }
            set { SetProperty(ref currentModuleName, value); }
        }
        private Hashtable ModuleNames { get; } = new Hashtable()  // 模块菜单项
        {
            {"FreePlayApp", "自由对弈" },
            {"CanJuPoJieApp", "残局破解" },
            {"CanJuSheJiApp", "残局设计" },
            {"FuPanApp", "棋谱复盘" },
            {"GuPuApp", "古谱研究" },
            {"QiPuWindow","棋谱记录" },
            {"UserQiPu","棋谱仓库" },
            {"MessageLogView", "消息记录"},
            {"NumberGameApp","数字游戏" }
        };
        private string GetModuleName(string key) => ModuleNames.ContainsKey(key) ? ModuleNames[key].ToString() : null;
        
        public MainWindowViewModel(IRegionManager regionManager, IEventAggregator ea, IContainerProvider containerProvider)
        {
            this.Rgm = regionManager;
            this.Ea = ea;
            this.Cp = containerProvider;
            LastCommand = new DelegateCommand(Last);
            NextCommand = new DelegateCommand(Next);
            RestartCommand = new DelegateCommand(Restart);
            FanZhuanCommand = new DelegateCommand(FanZhuanQiPan);
            // 容器中的单实例

            var engine = Cp.Resolve<UcciInfo>();
            SubMenuCommand = new DelegateCommand<string>(SubMenuClick);

            CurrentModuleName = "中国象棋 V5.0  " + GetModuleName("FreePlayApp");
            isDarkMode= Settings.Default.IsDarkTheme;
            ModifyTheme(isDarkMode);
            //regionNavigationJournal = Rgm.Regions["ContentRegion"].NavigationService.Journal;
        }

        private void SubMenuClick(string obj)
        {
            Rgm.Regions["ContentRegion"].RequestNavigate(obj, callback =>
            {
                if (callback.Result != null)
                    regionNavigationJournal = callback.Context.NavigationService.Journal;
            });
            CurrentModuleName = "中国象棋 V5.0  " + GetModuleName(obj);
        }
        private void Last()
        {
            if (regionNavigationJournal != null && regionNavigationJournal.CanGoBack)
            {
                regionNavigationJournal.GoBack();
                string str = regionNavigationJournal.CurrentEntry.Uri.OriginalString; // 获取当前导航区的模块名称
                CurrentModuleName = "中国象棋 V5.0  " + GetModuleName(str);
            }
        }
        private void Next()
        {
            if (regionNavigationJournal != null && regionNavigationJournal.CanGoForward)
            {
                regionNavigationJournal.GoForward();
                string str = regionNavigationJournal.CurrentEntry.Uri.OriginalString;
                CurrentModuleName = "中国象棋 V5.0  " + GetModuleName(str);
            }
        }

        private void Restart()
        {
            Ea.GetEvent<SystemMessageEvent>().Publish(new SystemMessage()
            {
                MessageName = "reStart",
                SenderFrom = this.GetType().Name,
                Content = "重新开局"
            });
        }
        private void FanZhuanQiPan()
        {
            Ea.GetEvent<SystemMessageEvent>().Publish(new SystemMessage()
            {
                MessageName = "qipanInverse",
                SenderFrom = this.GetType().Name,
                Content = "棋盘上下翻转"
            });
        }
        private static void ModifyTheme(bool isDarkTheme)
        {
            var paletteHelper = new PaletteHelper();
            var theme = paletteHelper.GetTheme();

            theme.SetBaseTheme(isDarkTheme ? Theme.Dark : Theme.Light);
            paletteHelper.SetTheme(theme);
            Settings.Default.IsDarkTheme = isDarkTheme;
            Settings.Default.Save();
        }
    }
}
